#!/usr/bin/make -f

DEB_SHLIBDEPS_INCLUDE := $(shell octave-config -p OCTLIBDIR) /usr/lib/atlas $(CURDIR)/src/libshogun $(CURDIR)/src/libshogunui
DEB_COMPRESS_EXCLUDE  := .py .html .R .m .sg
DEB_CONFIGURE_EXTRA_FLAGS += --disable-cpudetection --prefix=/usr
R_LIBRARY_DIR         := /usr/lib/R/site-library
LIBSHOGUN := libshogun4
LIBSHOGUNUI := libshogunui2

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/octave/debian/defs.make

PYVER     = $(shell pyversions -vr debian/control)
PYSTEST    = $(MAKE) -C src/python tests \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular;
PYMTEST    = $(MAKE) -C src/python_modular tests \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular;
PYSINSTALL = $(MAKE) -C src/python install \
			DESTDIR=$(CURDIR)/debian/shogun-python;
PYMINSTALL = $(MAKE) -C src/python_modular install \
			DESTDIR=$(CURDIR)/debian/shogun-python-modular;

patch: apply-patches

# doc
configure:: debian/stamp-configure
debian/stamp-configure:
	cd src && ./configure --disable-cpudetection --prefix=/usr --python=python$(PYVER)\
		--interfaces=libshogun,libshogunui,cmdline,r,octave,python,elwms,python_modular,octave_modular
	touch $@

build-arch: configure debian/stamp-build
debian/stamp-build:
	$(MAKE) -C src
	$(MAKE) -C doc
	touch $@

install/shogun-doc:: debian/stamp-install-doc
debian/stamp-install-doc:
	mkdir -p $(CURDIR)/debian/shogun-doc/usr/share/doc/shogun-doc/html
	cd doc/html && cp *.html *.png *.gif *.css $(CURDIR)/debian/shogun-doc/usr/share/doc/shogun-doc/html/
	touch $@

#libshogun
install/$(LIBSHOGUN):: debian/stamp-install-$(LIBSHOGUN)
debian/stamp-install-$(LIBSHOGUN):
	mkdir -p $(CURDIR)/debian/$(LIBSHOGUN)
	$(MAKE) -C src/libshogun install \
		DESTDIR=$(CURDIR)/debian/$(LIBSHOGUN) 
	cp -p src/libshogun/*.so.* $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib
	$(RM) -r $(CURDIR)/debian/$(LIBSHOGUN)/usr/include
	$(RM) $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib/libshogun.so
	$(RM) $(CURDIR)/debian/$(LIBSHOGUN)/usr/lib/libshogun.a
	touch $@

#libshogun-dev
install/libshogun-dev:: debian/stamp-install-libshogun-dev
debian/stamp-install-libshogun-dev:
	mkdir -p $(CURDIR)/debian/libshogun-dev
	$(MAKE) -C src/libshogun install \
		DESTDIR=$(CURDIR)/debian/libshogun-dev
	$(RM) -r $(CURDIR)/debian/libshogun-dev/usr/include/shogun/ui
	$(RM) $(CURDIR)/debian/libshogun-dev/usr/lib/libshogun.so.*
	touch $@

#libshogunui
install/$(LIBSHOGUNUI):: debian/stamp-install-$(LIBSHOGUNUI)
debian/stamp-install-$(LIBSHOGUNUI):
	mkdir -p $(CURDIR)/debian/$(LIBSHOGUNUI)
	$(MAKE) -C src/libshogunui install \
		DESTDIR=$(CURDIR)/debian/$(LIBSHOGUNUI) 
	cp -p src/libshogunui/*.so.* $(CURDIR)/debian/$(LIBSHOGUNUI)/usr/lib
	$(RM) -r $(CURDIR)/debian/$(LIBSHOGUNUI)/usr/include
	$(RM) $(CURDIR)/debian/$(LIBSHOGUNUI)/usr/lib/libshogunui.so
	$(RM) $(CURDIR)/debian/$(LIBSHOGUNUI)/usr/lib/libshogunui.a
	touch $@

#libshogunui-dev
install/libshogunui-dev:: debian/stamp-install-libshogunui-dev
debian/stamp-install-libshogunui-dev:
	mkdir -p $(CURDIR)/debian/libshogunui-dev
	$(MAKE) -C src/libshogunui install \
		DESTDIR=$(CURDIR)/debian/libshogunui-dev
	$(RM) $(CURDIR)/debian/libshogunui-dev/usr/lib/libshogunui.so.*
	touch $@

#cmdline
install/shogun-cmdline:: debian/stamp-install-cmdline
debian/stamp-install-cmdline:
	mkdir -p $(CURDIR)/debian/shogun-cmdline
	$(MAKE) -C src/cmdline install \
		DESTDIR=$(CURDIR)/debian/shogun-cmdline 
	touch $@

#octave
install/shogun-octave:: debian/stamp-install-octave
debian/stamp-install-octave:
	dh_install -pshogun-octave src/octave/sg.oct $(OCTDIR)
	octave-depends
	#$(MAKE) -C build-octave tests DESTDIR=$(CURDIR)/debian/shogun-octave
	touch $@

# octave-modular
install/shogun-octave-modular:: debian/stamp-install-octave-modular
debian/stamp-install-octave-modular:
	$(MAKE) -C src/octave_modular install \
		DESTDIR=$(CURDIR)/debian/shogun-octave-modular 
	octave-depends
	#$(MAKE) -C build-octave-modular tests \
		DESTDIR=$(CURDIR)/debian/shogun-octave-modular
	touch $@

# python-modular
install/shogun-python-modular:: debian/stamp-install-python-modular
debian/stamp-install-python-modular: modular=-modular
debian/stamp-install-python-modular:
	set -x; $(foreach pyver, $(PYVER), $(PYMINSTALL))
	#set -x; $(foreach pyver, $(PYVER), $(PYTEST))
	dh_pycentral -pshogun-python-modular
	touch $@

# python
install/shogun-python:: debian/stamp-install-python
debian/stamp-install-python: modular=
debian/stamp-install-python:
	set -x; $(foreach pyver, $(PYVER), $(PYSINSTALL))
	#set -x; $(foreach pyver, $(PYVER), $(PYTEST))
	dh_pycentral -pshogun-python
	touch $@

# elwms
install/shogun-elwms:: debian/stamp-install-elwms
debian/stamp-install-elwms:
	$(MAKE) DESTDIR=$(CURDIR)/debian/shogun-elwms RDIR=/usr/lib/R/site-library -C src/elwms install 
	dh_pycentral -pshogun-python
	octave-depends
	touch $@

# r
install/shogun-r:: debian/stamp-install-r
debian/stamp-install-r:
	$(MAKE) -C src/r install \
			DESTDIR=$(CURDIR)/debian/shogun-r \
			RDIR=/usr/lib/R/site-library
	#$(MAKE) -C src/r tests \
			DESTDIR=$(CURDIR)/debian/shogun-r \
			RDIR=/usr/lib/R/site-library;
	touch $@

get-orig-source:
	uscan --rename --force-download
	@echo successfully retrieved upstream tarball

clean::
	$(RM) debian/stamp-*
	$(RM) debian/shogun
	find debian -mindepth 2 -name 'shogun-*' -delete
